﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using System.Windows.Forms;
using System.IO;
using WMPLib;

namespace ShadowCommando.Game.GameSounds
{
    /// <summary>
    /// Klasa odpowiadająca  za obsługę dźwięków
    /// </summary>
    public class GameSound
    {
        private bool soundMute=false;
        private Dictionary<SoundID, string > soundInfoDictionary;
        private List<SoundInfo> listOfPlayers;
       
        /// <summary>
        /// Konstruktor
        /// </summary>
        public GameSound()
        {
            LoadSounds();
            listOfPlayers= new List<SoundInfo>();
            for(int i=0;i<10;i++)
            {
                listOfPlayers.Add(new SoundInfo());
            }
        }
        private void LoadSounds()
        {
            soundInfoDictionary = new Dictionary<SoundID, string>();
            string path = @"\Sounds\";
            path = Directory.GetCurrentDirectory() + path;
            soundInfoDictionary.Add(SoundID.Explosion, path + "Explosion.wav");
            soundInfoDictionary.Add(SoundID.Shot1, path + "Shot1.wav");
            soundInfoDictionary.Add(SoundID.Shot2, path + "Shot2.wav");

            soundInfoDictionary.Add(SoundID.Beep1, path + "Beep1.wav");
            soundInfoDictionary.Add(SoundID.Ahh, path + "Ahh.wav");
        }
        /// <summary>
        /// Odtwarza dźwięk
        /// </summary>
        /// <param name="id">Nazwa dźwięku</param>
        /// <param name="_loop">Czy powtarzać</param>
        public void PlaySound(SoundID id, bool _loop)
        {
            if (!soundMute)
            {
                SoundInfo si = listOfPlayers.FirstOrDefault(listOfPlayer => !listOfPlayer.IsBusy);
                if (si != null)
                {
                    si.URL = soundInfoDictionary[id];
                    si.Player.controls.play();

                }
            }


        }
        /// <summary>
        /// Wycisz dźwięki
        /// </summary>
        public bool SoundMute
        {
            get { return soundMute; }
            set { soundMute = value; }
        }

    }
    /// <summary>
    /// Informacje o dźwięku
    /// </summary>
    public class SoundInfo
    {

        private WMPLib.WindowsMediaPlayer player;
        /// <summary>
        /// Konstruktor
        /// </summary>
        public SoundInfo()
        {
            player = new WMPLib.WindowsMediaPlayer();
           
        }
        /// <summary>
        /// Player
        /// </summary>
        public WMPLib.WindowsMediaPlayer Player
        {

            get
            {
                return player;
            }
            set
            {
                player = value;
            }
        }
        /// <summary>
        /// Czy jest odtwarzany
        /// </summary>
        public bool IsBusy
        {
            get { return player.playState==WMPLib.WMPPlayState.wmppsPlaying?true:false; }
        }
        /// <summary>
        /// Ścieżka pliku
        /// </summary>
        public string URL
        {
            get { return player.URL; }
            set { player.URL = value; }
        }
        /// <summary>
        /// Graj dźwięk
        /// </summary>
        /// <param name="loop">Powtarzać</param>
        public void Play(bool loop)
        {

            player.settings.setMode("loop", loop);
            player.controls.play();
        }

            
    }
}
